﻿using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Data;
using LdtJudge.DataEngine.Model.QuteLawModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace LdtDataEngin.ModuleDetail.CommonModule.LawDeduce
{
    /// <summary>
    /// 查询法律法规信息
    /// </summary>
    public class QuoteLawExt
    {
        /// <summary>
        /// 数据基础库链接对象
        /// </summary>
        //private static DbHelperSQL oBase = DbHelperSQLFactory.CreateDatabase("FSLaw");
        //法律库
        // private static SqlCmmCommon sqlCmm = new SqlCmmCommon("Flaw");
        /// <summary>
        /// 新案例法律数据库api接口地址
        /// </summary>
        private static string urlpath = ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("LawDataBase");

        /// <summary>
        /// 查询法规ID
        /// </summary>
        /// <param name="catName">法规名称</param> 
        /// <param name="LawLibraryID">输出法律编号</param>
        /// <returns>法规ID</returns> 
        public static string GetLawID(string catName, out string LawLibraryID)
        {
            LawLibraryID = string.Empty;
            string apiPath = urlpath + @"/lawlibrary/law/catname";
            try
            {
                GetCatInIdParameterModel parameterModel = new GetCatInIdParameterModel();
                parameterModel.catName = catName;
                parameterModel.isLike = true;
                string jsonParameter = JsonConvert.SerializeObject(parameterModel);
                // 调用接口
                string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
                // 解析接口返回结果
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();
                if (result == "1")
                {
                    string bodyContent = jobject["body"].ToString();
                    var mJObj = JArray.Parse(bodyContent);
                    LawLibraryID = ((JObject)mJObj.First)["lc_LawLibraryID"].ToString().Substring(0, 1);
                    return ((JObject)mJObj.First)["catInID"].ToString();
                }
            }
            catch (Exception ex)
            {

            }

            return string.Empty;
        }

        /// <summary>
        /// 根据法规ID获取法规信息
        /// </summary>
        /// <param name="catInId">法规ID</param>
        /// <returns></returns>
        public static LawCatInfoModel GetLawInfo(string catInId)
        {
            LawCatInfoModel lawCatInfo = new LawCatInfoModel();
            try
            {
                string apiPath = urlpath + @"/lawlibrary/law/" + catInId;
                string message = HttpHelp.HttpGet(apiPath, "");
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();
                if (result == "1")
                {
                    lawCatInfo.catInID = jobject["body"]["catInID"].ToString();
                    lawCatInfo.catName = jobject["body"]["catName"].ToString();
                    lawCatInfo.effectiveDate = jobject["body"]["effectiveDate"].ToString();
                    lawCatInfo.hwType = jobject["body"]["hwType_add"].ToString();
                    lawCatInfo.lawlibraryID = jobject["body"]["lc_LawLibraryID"].ToString();
                    lawCatInfo.stFlag = jobject["body"]["stFlag"].ToString();
                }
            }
            catch (Exception ex)
            {
            }
            return lawCatInfo;
        }

        /// <summary>
        /// 查询法规ID
        /// </summary>
        /// <param name="catName">法规名称</param> 
        /// <param name="LawLibraryID">输出法律编号</param>
        //梵/ <returns>Tuple(法规ID,法规名称,生效日期,法规属性,法规类型)</returns> 
        public static Tuple<string, string, string, string, string> GetLawIDTupleModel(string catName, out string LawLibraryID)
        {
            LawLibraryID = string.Empty;
            string apiPath = urlpath + @"/lawlibrary/law/catname";
            try
            {
                if (!Regex.IsMatch(catName, "^[0-9]*$"))
                {
                    GetCatInIdParameterModel parameterModel = new GetCatInIdParameterModel();
                    parameterModel.catName = catName;
                    parameterModel.isLike = true;
                    string jsonParameter = JsonConvert.SerializeObject(parameterModel);
                    // 调用接口
                    string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
                    // 解析接口返回结果
                    JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                    string result = jobject["result"]["code"].ToString();
                    if (result == "1")
                    {
                        string bodyContent = jobject["body"].ToString();
                        var mJObj = JArray.Parse(bodyContent);
                        string lawID = ((JObject)mJObj.First)["catInID"].ToString();
                        string EffectiveDate = ((JObject)mJObj.First)["effectiveDate"].ToString();
                        string HwType = ((JObject)mJObj.First)["hwType_add"].ToString();
                        string STFlag = ((JObject)mJObj.First)["stFlag"].ToString();
                        LawLibraryID = ((JObject)mJObj.First)["lc_LawLibraryID"].ToString().Substring(0, 1);
                        return new Tuple<string, string, string, string, string>(lawID, catName, EffectiveDate, STFlag, HwType);
                    }
                }

            }
            catch (Exception ex)
            {

            }
            return new Tuple<string, string, string, string, string>("", "", "", "", "");
        }

        /// <summary>
        /// 根据法律ID获取法律实体信息
        /// </summary>
        /// <param name="lawID">法律实体信息</param>
        public static Dictionary<string, string> GetLawModelByID(string lawID)
        {
            string apiPath = urlpath + @"/lawlibrary/law/" + lawID;
            string message = HttpHelp.HttpGet(apiPath);
            JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
            string result = jobject["result"]["code"].ToString();
            if (result == "1")
            {

                string bodyContent = jobject["body"].ToString();
                return JsonConvert.DeserializeObject<Dictionary<string, string>>(bodyContent);
            }
            return null;
        }


        /// <summary>
        /// 根据法条ID获取法条信息
        /// </summary>
        /// <param name="lawID">法条ID</param>
        public static Dictionary<string, string> GetLawprovModelByID(string provInID)
        {
            string apiPath = urlpath + @"/lawlibrary/lawprov/" + provInID;
            string message = HttpHelp.HttpGet(apiPath);
            JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
            string result = jobject["result"]["code"].ToString();
            if (result == "1")
            {

                string bodyContent = jobject["body"].ToString();
                return JsonConvert.DeserializeObject<Dictionary<string, string>>(bodyContent);
            }
            return null;
        }
        /// <summary>
        /// 根据款ID获取款详细信息
        /// </summary>
        /// <param name="lawID">款ID</param>
        public static Dictionary<string, string> GetLawprovclauseModelByID(string clauseInNumber, string provInID, string itemNumber)
        {
            string apiPath = urlpath + @"/lawlibrary/lawprovclauseitem/getclauseiteminfo";

            GetClauseParameterModel parameterModel = new GetClauseParameterModel();
            parameterModel.provInID = provInID;
            parameterModel.clauseNumber = clauseInNumber;
            parameterModel.itemNumber = itemNumber;
            string jsonParameter = JsonConvert.SerializeObject(parameterModel);
            // 调用接口
            string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
            // 解析接口返回结果
            JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
            string result = jobject["result"]["code"].ToString();
            if (result == "1")
            {
                string bodyContent = jobject["body"].ToString();
                return JsonConvert.DeserializeObject<Dictionary<string, string>>(bodyContent);
            }
            return null;
        }
        public static string getpath()
        {
            string apiPath = urlpath + @"/lawlibrary/lawprovclauseitem/getclauseiteminfo";
            return apiPath;
        }
        /// <summary>
        /// 获取条文ID
        /// </summary>
        /// <param name="catID">法规编号</param>
        /// <param name="lawLibraryID">分库编号</param>
        /// <param name="provNumber"> 法律编号</param>
        /// <param name="provNumberzhi">包含之n的条款</param>
        /// <returns>条文编号</returns> 
        public static string GetProvID(string catID, string lawLibraryID, int provNumber, int provNumberzhi)
        {
            #region 对接法律API接口
            try
            {
                // api接口地址
                string apiPath = urlpath + @"/lawlibrary/lawprov/getprovinid";
                // 接口参数组装
                GetProvinIdParameterModel parameterModel = new GetProvinIdParameterModel();
                parameterModel.catInID = catID;
                parameterModel.provNumber = provNumber.ToString();
                parameterModel.subProvNumber = provNumberzhi.ToString();
                string jsonParameter = JsonConvert.SerializeObject(parameterModel);
                // 调用接口
                string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
                // 解析接口返回结果
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();
                if (result == "1")
                {
                    return jobject["body"]["provInID"].ToString();
                }
            }
            catch (Exception ex)
            {

            }
            #endregion
            return string.Empty;
        }

        /// <summary>
        /// 获取条文ID
        /// </summary>
        /// <param name="catID">法规编号</param>
        /// <param name="lawLibraryID">法律编号</param>
        /// <param name="provNumber">分库编号</param> 
        /// <returns>条文编号</returns> 
        public static string GetProvID(string catID, string lawLibraryID, int provNumber)
        {
            #region 对接法律API接口
            try
            {
                // api接口地址
                string apiPath = urlpath + @"/lawlibrary/lawprov/getprovinid";
                // 接口参数组装
                GetProvinIdParameterModel parameterModel = new GetProvinIdParameterModel();
                parameterModel.catInID = catID;
                parameterModel.provNumber = provNumber.ToString();
                parameterModel.subProvNumber = "";
                string jsonParameter = JsonConvert.SerializeObject(parameterModel);
                // 调用接口
                string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
                // 解析接口返回结果
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();
                if (result == "1")
                {
                    return jobject["body"]["provInID"].ToString();
                }
            }
            catch (Exception ex)
            {
            }
            #endregion
            return string.Empty;
        }

        /// <summary>
        /// 获取条文信息
        /// </summary>
        /// <param name="provId">条文ID</param>
        /// <returns></returns>
        public static LawProInfoModel GetProInfo(string provId)
        {
            LawProInfoModel lawProvInfo = new LawProInfoModel();
            try
            {
                string apiPath = urlpath + @"/lawlibrary/lawprov/" + provId;
                string message = HttpHelp.HttpGet(apiPath, "");
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();

                if (result == "1")
                    lawProvInfo = jobject["body"].ToObject<LawProInfoModel>();
            }
            catch (Exception ex)
            {
            }
            return lawProvInfo;
        }

        /// <summary>
        /// 获取款项ID
        /// </summary>
        /// <param name="catID">法规编号</param>
        /// <param name="ProvID">条编号</param>
        /// <param name="ClauseNumber">款编号</param>
        /// <param name="ItemNumber">项编号</param>
        /// <param name="IsClause">是否存在项</param>
        /// <returns>款项编号</returns> 
        public static string GetClauseID(string catID, string ProvID, int ClauseNumber, int ItemNumber, bool IsClause)
        {
            #region 对接法律API接口
            try
            {
                // api接口地址
                string apiPath = urlpath + @"/lawlibrary/lawprovclauseitem/getclauseinid";
                // 接口参数组装
                GetClauseinIdParameterModel parameterModel = new GetClauseinIdParameterModel();
                parameterModel.provInID = ProvID;
                parameterModel.clauseNumber = ClauseNumber.ToString();
                parameterModel.ItemNumber = ItemNumber.ToString();
                string jsonParameter = JsonConvert.SerializeObject(parameterModel);
                // 调用接口
                string message = HttpHelp.CreatePostHttpResponse(apiPath, jsonParameter);
                // 解析接口返回结果
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();
                if (result == "1")
                {
                    return jobject["body"]["clauseInID"].ToString();
                }
            }
            catch (Exception ex)
            {
            }
            #endregion
            return string.Empty;
        }

        /// <summary>
        /// 获取款信息
        /// </summary>
        /// <param name="clauseId">款ID</param>
        /// <returns></returns>
        public static LawClauseInfo GetClauseInfo(string clauseId)
        {
            LawClauseInfo lawClauseInfo = new LawClauseInfo();
            try
            {
                string apiPath = urlpath + @"/lawlibrary/lawprovclauseitem/" + clauseId;
                string message = HttpHelp.HttpGet(apiPath, "");
                JObject jobject = (JObject)JsonConvert.DeserializeObject(message);
                string result = jobject["result"]["code"].ToString();

                if (result == "1")
                    lawClauseInfo = jobject["body"].ToObject<LawClauseInfo>();
            }
            catch (Exception ex)
            {
            }
            return lawClauseInfo;
        }
    }
}
